package com.songtech.server.netty.Initializer;

import com.songtech.server.netty.handler.ServerHandler;
import com.songtech.web.entry.SysPosElefence;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Create By YINN on 2018/4/3 16:11
 * Description : 处理器链
 */

@Service("serverInitializer")
public class ServerInitializer extends ChannelInitializer<SocketChannel> {

    @Autowired
    private ServerHandler serverHandler;

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();


        // 以("#")为结尾分割的 解码器,同时去除#
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(2048, true, false, Unpooled.copiedBuffer("#".getBytes())));

        // 字符串解码 和 编码
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());


        // 自己的逻辑Handler
        pipeline.addLast("handler", serverHandler);
    }
}
